package com.base.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/**
 * 图片工具类
 *
 * @author cjq on 2019/10/12
 */
public class PhotoUtil {
    /**
     * 日志打印
     */
    private static final Logger logger = LoggerFactory.getLogger(PhotoUtil.class);

    /**
     * 压缩图片
     *
     * @param imgByte      图片流
     * @param compressSize 压缩到指定大小内  单位KB
     * @return
     * @throws IOException
     */
    public static byte[] compress(byte[] imgByte, int compressSize) throws IOException {
        if (imgByte.length < (compressSize * 1024)) {
            return imgByte;
        } else {
            double scale = getScale(imgByte.length);
            BufferedImage bi = ImageIO.read(new ByteArrayInputStream(imgByte));
            int width = (int) (bi.getWidth() * scale);
            int height = (int) (bi.getHeight() * scale);
            Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics graphics = tag.getGraphics();
            graphics.setColor(Color.RED);
            graphics.drawImage(image, 0, 0, null);
            graphics.dispose();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(tag, "png", byteArrayOutputStream);
            return compress(byteArrayOutputStream.toByteArray(), compressSize);
        }

    }

    /**
     * 获取压缩系数
     *
     * @param length
     * @return
     */
    private static double getScale(int length) {
        double sacle = 1.0;
        if (length > (1024 * 1024)) {
            sacle = 0.1;
        } else if (length > (500 * 1024)) {
            sacle = 0.3;
        } else if (length > (300 * 1024)) {
            sacle = 0.45;
        } else if (length > (250 * 1024)) {
            sacle = 0.6;
        } else if (length > (200 * 1024)) {
            sacle = 0.65;
        } else if (length > (150 * 1024)) {
            sacle = 0.75;
        } else if (length > (100 * 1024)) {
            sacle = 0.85;
        } else if (length > (50 * 1024)) {
            sacle = 0.95;
        } else {
            sacle = 0.97;
        }
        return sacle;
    }

    /**
     * 将byte数据转换成base64编码的字符串
     * @param bytes
     * @return
     * @throws IOException
     */
    public static String changeByteArrayToBase64(byte[] bytes)throws IOException{
        if(bytes==null){
            return "";
        }
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        BufferedImage bi = ImageIO.read(inputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(bi,"png",byteArrayOutputStream);
        byte[] bytes1 = byteArrayOutputStream.toByteArray();
        return new BASE64Encoder().encodeBuffer(bytes1).trim();
    }


}
